#!/usr/bin/python
#coding=utf-8

# Project: OaixLouder
# Author : Xiao Xiangquan
# Date   : 2010.11.12

import os
import Tkinter
import thread
import sys
from client import *
from SimpleDialog import *
import pickle

reload(sys)
sys.setdefaultencoding('utf-8')

config_dict = {}
config_file = "config.pk"
clients = {}

def save_config():
    for client_name in clients.keys():
        clients[client_name].update_config()
        config_dict[client_name] = clients[client_name].config
    pickle.dump( config_dict, open(config_file,"w") )

def build_ui( root ):
    checkbutton_blogs = {}
    checkbutton_microblogs = {}
    checkbutton_forums = {}
    
    root.title("Oaix Louder")
    root.resizable( False, False )
############################## Frame 1
    frame1 = Tkinter.Frame( root )
    frame1.grid( row=0 )

    Tkinter.Label( frame1, text="标题" ).grid( row=0, column=0, sticky=Tkinter.W )
    entry_title = Tkinter.Entry( frame1, width=79 )
    entry_title.grid( row=0, column=1, sticky=Tkinter.W )
    
    Tkinter.Label( frame1, text="内容" ).grid( row=1, column=0, sticky=Tkinter.W )
    text_content = Tkinter.Text( frame1, width=90, height=30 )
    text_content.grid( row=1, column=1, sticky=Tkinter.W )
#############################
    Tkinter.Label( root, text="我要广播到以下目标" ).grid( row=1 )
############################# Frame 2
    frame2 = Tkinter.Frame( root )
    frame2.grid( row=2, sticky=Tkinter.W )

    line = 0
    Tkinter.Label( frame2, text="博客" ).grid( row=line, sticky=Tkinter.W )
    line += 1
    for client_name in blog:
        client_frame = Tkinter.Frame( frame2 )
        client_frame.grid( row=line, sticky=Tkinter.W )
        
        var = Tkinter.IntVar()
        checkbutton = Tkinter.Checkbutton( client_frame, variable=var )
        checkbutton.grid( column=0, sticky=Tkinter.W )
        checkbutton_blogs[client_name] = ( var, checkbutton )

        clients[client_name].display_blog_option( client_frame )
        line += 1

    Tkinter.Label( frame2, text="论坛" ).grid( row=line, sticky=Tkinter.W )
    line += 1
    for client_name in forum:
        client_frame = Tkinter.Frame( frame2 )
        client_frame.grid( row=line, sticky=Tkinter.W )
        
        var = Tkinter.IntVar()
        checkbutton = Tkinter.Checkbutton( client_frame, variable=var )
        checkbutton.grid( column=0, sticky=Tkinter.W )
        checkbutton_forums[client_name] = ( var, checkbutton )

        clients[client_name].display_forum_option( client_frame )
        line += 1

    Tkinter.Label( frame2, text="微博" ).grid( row=line, sticky=Tkinter.W )
    line += 1
    for client_name in microblog:
        client_frame = Tkinter.Frame( frame2 )
        client_frame.grid( row=line, sticky=Tkinter.W )
        
        var = Tkinter.IntVar()
        checkbutton = Tkinter.Checkbutton( client_frame, variable=var )
        checkbutton.grid( column=0, sticky=Tkinter.W )
        checkbutton_microblogs[client_name] = ( var, checkbutton )

        clients[client_name].display_microblog_option( client_frame )
        line += 1

############################# Frame 3
    def on_submit():
        title = entry_title.get().decode('utf-8')
        content = text_content.get('1.0', Tkinter.END ).decode('utf-8') # all content
        
        if len(content)>0 and len(title)>0:
            for client_name in checkbutton_blogs.keys():
                if 1==checkbutton_blogs[client_name][0].get():
                    print "Process", client_name
                    clients[client_name].post_blog(title, content)
#                    thread.start_new_thread( clients[client_name].post_blog, (title, content) )
            for client_name in checkbutton_forums.keys():
                if 1==checkbutton_forums[client_name][0].get():
                    print "Process", client_name
                    clients[client_name].post_topic( title, content)
#                    thread.start_new_thread( clients[client_name].post_topic, ( title, content) )
        if len(content)>0:
            for client_name in checkbutton_microblogs.keys():
                if 1==checkbutton_microblogs[client_name][0].get():
                    print "Process", client_name
                    clients[client_name].post_microblog( content)
#                    thread.start_new_thread( clients[client_name].post_microblog, ( content) )
    def on_help():
        SimpleDialog( root, text = '''
使用方法：
运行程序，输入标题（发表博客或帖子时需要）、内容（必须）等。
勾选希望发布到的目标，点击“开始广播”，然后就可以去看看有没有效果了～
''', buttons = ['Yes']).go()

    def on_about():
        SimpleDialog( root, text = '''
关于软件：纯属练手
关于作者：纯属酱油
    ——OaixLouder由Oaix出品，谢谢关注
http://code.google.com/p/oaixlouder
''', buttons = ['Yes']).go()

    frame3 = Tkinter.Frame( root )
    frame3.grid( row=3 )
    Tkinter.Button( frame3, width=10, text="开始广播", command=on_submit ).grid( row=0, column=0 )
    Tkinter.Button( frame3, width=10, text="使用说明", command=on_help ).grid( row=0, column=1 )
    Tkinter.Button( frame3, width=10, text="关于", command=on_about ).grid( row=0, column=2 )

#########################################
if __name__=='__main__':
    # read the config file
    if len(sys.argv)==2:
        config_file = sys.argv[1]
    if os.path.exists( config_file ):
        file = open( config_file, "r" )
        config_dict = pickle.load( file )
        file.close()

    for client_name in blog+forum+microblog:
        if not clients.has_key( client_name ):
            if config_dict.has_key(client_name):
                exec "c = "+ client_name +".client( config_dict[client_name] )"
            else:
                exec "c = "+ client_name +".client()"
            clients[client_name] = c

    root = Tkinter.Tk()
    build_ui(root)
    root.mainloop()
    save_config()
